package com.tensquare.spit.controller;

import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import sun.security.provider.ConfigFile;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {
    @Autowired
    private SpitService spitService;
    /**
     * 新增吐槽
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result saveSpit(@RequestBody Spit spit){
        spitService.saveSpit(spit);
        return new Result(StatusCode.OK,"新增成功",true);
    }

    /**
     * 查询所有
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Spit> list= spitService.findAll();
        return new Result(StatusCode.OK,"查询成功",true,list);
    }

    /**
     * 根据id查询
     */
    @RequestMapping(value = "/{spitId}",method = RequestMethod.GET)
    public Result findById(@PathVariable String spitId){
        Spit spit= spitService.findById(spitId);
        return new Result(StatusCode.OK,"查询成功",true,spit);
    }

    /**
     * 根据id删除
     */
    @RequestMapping(value = "/{spitId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String spitId){
        spitService.deleteById(spitId);
        return new Result(StatusCode.OK,"查询成功",true);
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/{spitId}",method = RequestMethod.PUT)
    public Result update(@PathVariable String spitId,@RequestBody Spit spit){
        spit.set_id(spitId);
        spitService.update(spit);
        return new Result(StatusCode.OK,"查询成功",true);
    }
    /**
     * 吐槽点赞
     */
    @RequestMapping(value = "/thumbup/{spitId}",method = RequestMethod.PUT)
    public Result adThumbup(@PathVariable String spitId){
        return spitService.adThumbup(spitId);
    }

    /**
     * 根据条件查询Spit列表
     */
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public List<Spit> searchSpitList(@RequestBody Spit spit){
        return null;
    }

    /**
     *Spit分页
     */
    @RequestMapping(value = "/search/{page}/{size}",method = RequestMethod.POST)
    public Result searchSpitByPage(@RequestBody Spit spit, @PathVariable Integer page,@PathVariable Integer size){
        return new Result();
    }

    /**
     * 根据上级ID查询吐槽数据（分页）
     */
    @RequestMapping(value = "/comment/{parentid}/{page}/{size}",method = RequestMethod.GET)
    public Result searchSpitPage(@PathVariable String parentid,@PathVariable Integer page,@PathVariable Integer size){
        Page<Spit> spits = spitService.searchSpitPage(parentid, page, size);
        return new Result(StatusCode.OK,"查询成功",true,new PageResult<>(spits.getTotalElements(),spits.getContent()));
    }
}
